﻿using System;
using System.Web.UI.WebControls;

using System.Data;
using System.Data.SqlClient;
using System.Text;

namespace IFD2
{
    public partial class FareDetails : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
                return;

            if (Session["ACL"].ToString().IndexOf("SA3") < 0)
            {
                lblError.Text = "Unauthorized!";
                return;
            }

            try
            {
                int dbf;
                string fare_currency;
                SqlCommand cmd = new SqlCommand();
                SqlParameter para;
                DataTable dt_tmp = new DataTable();
                cmd.CommandText = "bll_query_get_fare_details";
                dbf = Convert.ToInt16(Request.QueryString["DBF"]);
                para = cmd.Parameters.AddWithValue("@airline", Request.QueryString["AIRLINE"]);
                para = cmd.Parameters.AddWithValue("@fare_code", Request.QueryString["FARE_CODE"]);
                para = cmd.Parameters.AddWithValue("@ori_airport", Request.QueryString["ORI_AIRPORT"]);
                para = cmd.Parameters.AddWithValue("@des_airport", Request.QueryString["DES_AIRPORT"]);
                //para = cmd.Parameters.AddWithValue("@flt_tax_type", Request.QueryString["FLT_TAX_TYPE"]);
                //para = cmd.Parameters.AddWithValue("@tax_valid", Request.QueryString["TAX_VALID"]);
                //para = cmd.Parameters.AddWithValue("@surcharge_valid", Request.QueryString["SURCHARGE_VALID"]);
                para = cmd.Parameters.AddWithValue("@dbf", dbf);
                para = cmd.Parameters.AddWithValue("@lang", Request.QueryString["LANG"]);

                TinyDAL.Exec(ref cmd, ref dt_tmp);
                if (dt_tmp.Rows.Count == 1)
                {
                    lblFareName.Text = dt_tmp.Rows[0]["fare_name"].ToString();
                    lblFareCode.Text = dt_tmp.Rows[0]["code"].ToString();
                    lblLine.Text = dt_tmp.Rows[0]["trip_line"].ToString();
                    lblSeatMin.Text = dt_tmp.Rows[0]["seats_min"].ToString();
                    lblStayMin.Text = dt_tmp.Rows[0]["stay_min"].ToString();
                    lblStayMax.Text = dt_tmp.Rows[0]["stay_max"].ToString();
                    //lblChgNameFee.Text = dt_tmp.Rows[0]["chg_name_fee"].ToString();
                    //lblChgNameBefore.Text = dt_tmp.Rows[0]["chg_name_before"].ToString();
                    //lblChgFltFee.Text = dt_tmp.Rows[0]["chg_flt_fee"].ToString();
                    //lblChgFltBefore.Text = dt_tmp.Rows[0]["chg_flt_before"].ToString();
                    fare_currency = dt_tmp.Rows[0]["currency"].ToString();
                    lblPrice.Text = dt_tmp.Rows[0]["currency"] + Convert.ToDecimal(dt_tmp.Rows[0]["adt_price"]).ToString("0");
                    //lblAirportTax.Text = dt_tmp.Rows[0]["currency"] + Convert.ToDecimal(dt_tmp.Rows[0]["adt_taxes"]).ToString("0");
                    //lblFuelCharge.Text = dt_tmp.Rows[0]["currency"] + Convert.ToDecimal(dt_tmp.Rows[0]["adt_surcharge"]).ToString("0");
                    lblFreeBaggage.Text = dt_tmp.Rows[0]["free_baggage"].ToString();

                    lblChdPrice.Text = dt_tmp.Rows[0]["currency"] + Convert.ToDecimal(dt_tmp.Rows[0]["chd_price"]).ToString("0");
                    //lblChdTax.Text = dt_tmp.Rows[0]["currency"] + Convert.ToDecimal(dt_tmp.Rows[0]["chd_taxes"]).ToString("0");
                    //lblChdFuel.Text = dt_tmp.Rows[0]["currency"] + Convert.ToDecimal(dt_tmp.Rows[0]["chd_surcharge"]).ToString("0");
                    lblChdBaggage.Text = dt_tmp.Rows[0]["chd_baggage"].ToString();

                    lblInfPrice.Text = dt_tmp.Rows[0]["currency"] + Convert.ToDecimal(dt_tmp.Rows[0]["inf_price"]).ToString("0");
                    //lblInfTax.Text = dt_tmp.Rows[0]["currency"] + Convert.ToDecimal(dt_tmp.Rows[0]["inf_taxes"]).ToString("0");
                    //lblInfFuel.Text = dt_tmp.Rows[0]["currency"] + Convert.ToDecimal(dt_tmp.Rows[0]["inf_surcharge"]).ToString("0");
                    lblInfBaggage.Text = dt_tmp.Rows[0]["inf_baggage"].ToString();

                    lblRules.Text = dt_tmp.Rows[0]["fare_rule"].ToString();
                }
                else
                {
                    lblRules.Text = "Duplicated Fare or Fare Not Found! Call fare manager.";
                    return;
                }

                TableRow tabRow;
                TableCell tabCell;

                cmd.Parameters.Clear();
                cmd.CommandText = "bll_query_get_fare_approved_stairs";
                para = cmd.Parameters.AddWithValue("@airline", Request.QueryString["AIRLINE"]);
                para = cmd.Parameters.AddWithValue("@fare_code", Request.QueryString["FARE_CODE"]);
                para = cmd.Parameters.AddWithValue("@seg_sn", Request.QueryString["SEG_SN"]);
                para = cmd.Parameters.AddWithValue("@user_airline", Session["AIRLINE"].ToString());
                para = cmd.Parameters.AddWithValue("@office", Session["OFFICE"].ToString());
                para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());
                TinyDAL.Exec(ref cmd, ref dt_tmp);
                int height;
                tabRow = new TableRow();
                foreach (DataRow row in dt_tmp.Rows)
                {
                    height = Convert.ToInt16(row["adt_price"]) / 20;
                    if (height < 10)
                        height = 10;
                    tabCell = new TableCell();
                    if (dbf == Convert.ToInt16(row["fltdate_diff"]))
                        tabCell.Text = Convert.ToSingle(row["adt_price"]).ToString()+"<br/><img src='images/bar_blue.bmp' width=10 height=" + height.ToString() + "><br/>" + Convert.ToInt16(row["fltdate_diff"]).ToString("+#;-#;0");
                    else
                        tabCell.Text = Convert.ToSingle(row["adt_price"]).ToString() + "<br/><img src='images/bar_green.bmp' width=10 height=" + height.ToString() + "><br/>" + Convert.ToInt16(row["fltdate_diff"]).ToString("+#;-#;0");
                    tabCell.VerticalAlign = VerticalAlign.Bottom;
                    tabCell.HorizontalAlign = HorizontalAlign.Center;
                    tabCell.Width = 70;
                    tabRow.Cells.Add(tabCell);
                }
                tabAdtPrice.Rows.Add(tabRow);

                tabRow = new TableRow();
                foreach (DataRow rowChdPrice in dt_tmp.Rows)
                {
                    height = Convert.ToInt16(rowChdPrice["chd_price"]) / 20;
                    if (height < 10)
                        height = 10;
                    tabCell = new TableCell();
                    if (dbf == Convert.ToInt16(rowChdPrice["fltdate_diff"]))
                        tabCell.Text = Convert.ToSingle(rowChdPrice["chd_price"]).ToString() + "<br/><img src='images/bar_blue.bmp' width=10 height=" + height.ToString() + "><br/>" + Convert.ToInt16(rowChdPrice["fltdate_diff"]).ToString("+#;-#;0");
                    else
                        tabCell.Text = Convert.ToSingle(rowChdPrice["chd_price"]).ToString() + "<br/><img src='images/bar_green.bmp' width=10 height=" + height.ToString() + "><br/>" + Convert.ToInt16(rowChdPrice["fltdate_diff"]).ToString("+#;-#;0");
                    tabCell.VerticalAlign = VerticalAlign.Bottom;
                    tabCell.HorizontalAlign = HorizontalAlign.Center;
                    tabCell.Width = 70;
                    tabRow.Cells.Add(tabCell);
                }
                tabChdPrice.Rows.Add(tabRow);

                tabRow = new TableRow();
                foreach (DataRow rowInfPrice in dt_tmp.Rows)
                {
                    height = Convert.ToInt16(rowInfPrice["inf_price"]) / 20;
                    if (height < 10)
                        height = 10;
                    tabCell = new TableCell();
                    if (dbf == Convert.ToInt16(rowInfPrice["fltdate_diff"]))
                        tabCell.Text = Convert.ToSingle(rowInfPrice["inf_price"]).ToString()+"<br/><img src='images/bar_blue.bmp' width=10 height=" + height.ToString() + "><br/>" + Convert.ToInt16(rowInfPrice["fltdate_diff"]).ToString("+#;-#;0");
                    else
                        tabCell.Text = Convert.ToSingle(rowInfPrice["inf_price"]).ToString() + "<br/><img src='images/bar_green.bmp' width=10 height=" + height.ToString() + "><br/>" + Convert.ToInt16(rowInfPrice["fltdate_diff"]).ToString("+#;-#;0");
                    tabCell.VerticalAlign = VerticalAlign.Bottom;
                    tabCell.HorizontalAlign = HorizontalAlign.Center;
                    tabCell.Width = 70;
                    tabRow.Cells.Add(tabCell);
                }
                tabInfPrice.Rows.Add(tabRow);

                int i;
                dt_tmp.Clear();
                StringBuilder tmp=new StringBuilder();
                cmd.Parameters.Clear();
                cmd.CommandText = "bll_query_get_flt_surcharge_details";
                para = cmd.Parameters.AddWithValue("@airline", Request.QueryString["AIRLINE"]);
                para = cmd.Parameters.AddWithValue("@flt_no", Request.QueryString["FLT_NO"]);
                para = cmd.Parameters.AddWithValue("@flt_date", Request.QueryString["FLT_DATE"]);
                para = cmd.Parameters.AddWithValue("@seg_sn", Request.QueryString["SEG_SN"]);
                para = cmd.Parameters.AddWithValue("@fare_currency", fare_currency);
                para = cmd.Parameters.AddWithValue("@lang", Session["LANG"].ToString());
                i = TinyDAL.Exec(ref cmd, ref dt_tmp);

                if (i==1)
                {
                    lblError.Text = "Flight not found!";
                    return;
                }
                else if(i==2)
                {
                    lblError.Text = "Taxes & surcharges not found!";
                    return;
                }
                else if (i == 0)/*dt_tmp.Rows.Count>0*/
                {
                    tmp.Append("<tr><td colspan='7'>Taxes and Surcharges</td></tr><tr><td>Surcharge Code</td><td>Name</td><td>Adt. Price</td><td>Chd. Price</td><td>Inf. Price</td></tr>");
                    foreach (DataRow row in dt_tmp.Rows)
                    {
                        tmp.Append("<tr><td>&bull; " + row["code"].ToString().TrimEnd() + "(" + row["type"].ToString() + ")</td><td>" + row["name"].ToString().TrimEnd() + "</td><td>"
                            + row["currency"].ToString() + row["adt_price"].ToString() + "(≈" + row["fare_currency"].ToString() + (Convert.ToDecimal(row["adt_price"]) * Convert.ToDecimal(row["exch_rate"])).ToString() + ")</td><td>"
                            + row["currency"].ToString() + row["chd_price"].ToString() + "(≈" + row["fare_currency"].ToString() + (Convert.ToDecimal(row["chd_price"]) * Convert.ToDecimal(row["exch_rate"])).ToString() + ")</td><td>"
                            + row["currency"].ToString() + row["inf_price"].ToString() + "(≈" + row["fare_currency"].ToString() + (Convert.ToDecimal(row["inf_price"]) * Convert.ToDecimal(row["exch_rate"])).ToString() + ")</td></tr>");
                    }
                    litSurcharges.Text = tmp.ToString();
                    return;
                }
                else
                {
                    lblError.Text = "Unknown error!";
                    return;
                }


            }
            catch (Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    lblError.Text=ex.Message;
                else
                    lblError.Text="Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";

                return;
            }
        }
    }
}